
package it.lucci.ricochet.gui;

import it.lucci.ricochet.Game;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import javax.swing.JPanel;

/**
 * @author Gabriele Lucci
 * @version 1.0
 * Copyright 2012, Gabriele Lucci
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
public class GamePane extends JPanel implements Runnable{

    private Game game;
    private Image dbi;
    private Graphics dbg;
    
    public int width, heigth;
    
    public static final RenderingHints ANTIALIASING = new RenderingHints(
            RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    /**
     * 
     * @param d le dimensioni di questo pannello
     */
    public GamePane(Dimension d) {
        this.setPreferredSize(d);
        this.game = new Game(this);
        this.addMouseListener(game);
        this.addMouseMotionListener(game);
        this.width = d.width;
        this.heigth = d.height;
    }

    @Override
    public void paint(Graphics g){
        // double buffering
        dbi = createImage(width, heigth);
        dbg = dbi.getGraphics();    
        
        paintComponent(dbg);
        g.drawImage(dbi, 0, 0, this);
    }
    
    /**
     *
     * @param g
     */
    @Override
    public void paintComponent(Graphics g){
        render(g);
    }
    
    public void render(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        g2.addRenderingHints(ANTIALIASING); 
        g2.setBackground(Color.black);
        g2.clearRect(0, 0, width, heigth);
        // disegna pallina
        g2.setPaint(Color.white);
        game.getBall().draw(g2);
        // disegna paddle
        g2.setPaint(Color.white.darker());
        game.getPad().draw(g2);
        // disegna punteggio
        g2.setPaint(Color.white);
        g2.drawString(""+game.score, getWidth()-40, 20);
    }

    /**
     * 
     * @return 
     */
    public Game getGame() {
        return game;
    }
    /**
     * 
     * @param game 
     */
    public void setGame(Game game) {
        this.game = game;
    }
    
    public void startGame(){
        new Thread(this, "Animation Thread").start();
        game.startGame();
    }

    @Override
    public void run() {
        try{
            while(true){
                repaint();
                Thread.sleep(20);   // 50Hz
            }
        } catch (Exception e) {
            System.err.print(e);
        }
    }
    

}
